package com.witframework.web.tag;


import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class FlashMessageTag extends BodyTagSupport{
  PageContext pageContext;
  private static final int DEFAULT_DELAY = 5000;
  private int delay = 5000;
  private boolean cond = true;
  private String sBody = null;
  private static final String FLASH_MESSAGE_TAG = "flshmsgtgcj2007";

  public void setCond(boolean paramBoolean)  {
    this.cond = paramBoolean;
  }

  public boolean getCond()
  {
    return this.cond;
  }

  public void setDelay(int paramInt)
  {
    this.delay = paramInt;
  }

  public int getDelay()
  {
    return this.delay;
  }

  public void setPageContext(PageContext paramPageContext)
  {
    this.pageContext = paramPageContext;
  }

  public int doAfterBody()
  {
    BodyContent localBodyContent = getBodyContent();
    if (localBodyContent != null)
    {
      this.sBody = localBodyContent.getString();
      localBodyContent.clearBody();
    }
    return 0;
  }

  public int doEndTag()    throws JspException  {
    StringBuffer localStringBuffer = new StringBuffer("");
    if (this.sBody == null)
      this.sBody = "";
    else
      this.sBody = this.sBody.trim();
    if (this.cond)    {
      Integer localInteger = (Integer)this.pageContext.getAttribute("flshmsgtgcj2007");
      if (localInteger == null)      {
        localInteger = new Integer(0);
        localStringBuffer.append(getJavaScriptCode());
      }
      localStringBuffer.append("<div");
      localStringBuffer.append(" id='" + localInteger + "'");
      localStringBuffer.append(" style='display:none'>");
      localStringBuffer.append(this.sBody);
      localStringBuffer.append("</div>");
      localStringBuffer.append("<script languge=\"JavaScript\" type=\"text/javascript\">");
      localStringBuffer.append("flashMessageFadeIn('" + localInteger + "',30,300," + this.delay + ",50,2000);");
      localStringBuffer.append("</script>");
      this.pageContext.setAttribute("flshmsgtgcj2007", new Integer(localInteger.intValue() + 1), 1);
    }
    else
    {
      localStringBuffer.append(this.sBody);
    }
    try
    {
      this.pageContext.getOut().write(localStringBuffer.toString());
    }
    catch (Exception localException)
    {
      throw new JspException("Could not save body " + localException.toString());
    }
    dropData();
    return 6;
  }

  public void release()
  {
    dropData();
  }

  private void dropData()
  {
    this.delay = 5000;
    this.cond = true;
  }

  private String getJavaScriptCode(){
    StringBuffer localStringBuffer = new StringBuffer("");
    localStringBuffer.append("<script languge=\"JavaScript\" type=\"text/javascript\">");
    localStringBuffer.append("function flashMessageSetOpacity(id,level) {");
    localStringBuffer.append("var element = document.getElementById(id);");
    localStringBuffer.append("element.style.display = 'inline';");
    localStringBuffer.append("element.style.zoom = 1;");
    localStringBuffer.append("element.style.opacity = level;");
    localStringBuffer.append("element.style.MozOpacity = level;");
    localStringBuffer.append("element.style.KhtmlOpacity = level;");
    localStringBuffer.append("element.style.filter = \"alpha(opacity=\" + (level * 100) + \");\";");
    localStringBuffer.append("}");
    localStringBuffer.append("function flashMessageFadeIn(id,steps,duration,interval,fadeOutSteps,fadeOutDuration){");
    localStringBuffer.append("var fadeInComplete;");
    localStringBuffer.append("for (i = 0; i <= 1; i += (1 / steps)) {");
    localStringBuffer.append("setTimeout(\"flashMessageSetOpacity('\" + id + \"', \" + i + \")\", i * duration);");
    localStringBuffer.append("fadeInComplete = i * duration;");
    localStringBuffer.append("}");
    localStringBuffer.append("setTimeout(\"flashMessageFadeOut('\" + id + \"', \" + fadeOutSteps + \", \" + fadeOutDuration + \")\", fadeInComplete + interval);");
    localStringBuffer.append("}");
    localStringBuffer.append("function flashMessageFadeOut(id, steps, duration) {");
    localStringBuffer.append("var fadeOutComplete;");
    localStringBuffer.append("for (i = 0; i <= 1; i += (1 / steps)) {");
    localStringBuffer.append("setTimeout(\"flashMessageSetOpacity('\" + id + \"', \"  + (1 - i) + \")\", i * duration);");
    localStringBuffer.append("fadeOutComplete = i * duration;");
    localStringBuffer.append("}");
    localStringBuffer.append("setTimeout(\"flashMessageHide('\" + id + \"')\", fadeOutComplete);");
    localStringBuffer.append("}");
    localStringBuffer.append("function flashMessageHide(id){");
    localStringBuffer.append("document.getElementById(id).style.display = 'none';");
    localStringBuffer.append("}");
    localStringBuffer.append("</script>");
    return "";//localStringBuffer.toString();
  }
}